//
//  RSBaseCommonViewController.m
//  RenShineDoctor
//
//  Created by 黄强强 on 15/11/6.
//  Copyright © 2015年 杭州岐黄信息技术有限公司. All rights reserved.
//

#import "HQQBaseSettingViewController.h"
#import "HQQSettingConfig.h"
#import "HQQSettingCell.h"
#import "HQQSettingGroup.h"
#import "HQQSettingItem.h"
#import "HQQSettingArrowItem.h"
#import "HQQSettingHeaderFooterView.h"
#import <Masonry.h>

@interface HQQBaseSettingViewController () <RSCommonCellDelegate, UITableViewDataSource, UITableViewDelegate>

@end

@implementation HQQBaseSettingViewController

- (NSMutableArray *)groups
{
    if (!_groups) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
}

- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - <UITableViewDelegate, UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HQQSettingGroup *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HQQSettingCell *cell = [HQQSettingCell cellWithTableView:tableView];
    cell.delegate = self;
    
    HQQSettingGroup *group = self.groups[indexPath.section];
    HQQSettingItem *item = group.items[indexPath.row];
    
    cell.item = item;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HQQSettingGroup *group = self.groups[indexPath.section];
    HQQSettingItem *item = group.items[indexPath.row];
    
    if (item.operation) {
        item.operation();
    }
    
    if ([item isKindOfClass:[HQQSettingArrowItem class]]) {
        
        HQQSettingArrowItem *arrowItem = (HQQSettingArrowItem *)item;
        UIViewController *vc = [[arrowItem.destVC alloc] init];
        vc.title = arrowItem.title;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HQQSettingGroup *group = self.groups[section];
    
    if (group.header) {
        return [self setupHeaderFooterViewWithText:group.header textAlignment:group.headerAlignment];
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    HQQSettingGroup *group = self.groups[section];
    
    if (group.footer) {
        return [self setupHeaderFooterViewWithText:group.footer textAlignment:group.footerAlignment];
    }
    
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    HQQSettingGroup *group = self.groups[section];
    return group.footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    HQQSettingGroup *group = self.groups[section];
    if (group.header) {
        return 30.0f;
    }
    return 10.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    HQQSettingGroup *group = self.groups[section];
    if (group.footer) {
        return 30.0f;
    }
    return 0.1;
}

/**
 *  创建headerFooterView
 *
 *  @param text          文字
 *  @param textAlignment 文字对齐方式
 */
- (HQQSettingHeaderFooterView *)setupHeaderFooterViewWithText:(NSString *)text textAlignment:(RSCommonGroupTextAlignment)textAlignment
{
    HQQSettingHeaderFooterView *headerView = [HQQSettingHeaderFooterView headerFooterViewWithTableView:self.tableView];
    headerView.titleLabel.text = text;
    if (textAlignment == RSCommonGroupTextAlignmentCenter) {
        headerView.titleLabel.textAlignment = NSTextAlignmentCenter;
    }else if (textAlignment == RSCommonGroupTextAlignmentRight) {
        headerView.titleLabel.textAlignment = NSTextAlignmentRight;
    }
    return headerView;
}

- (void)commonCellDidChangeSwitch:(id)currentSwitch
{
    
}

@end
